package com.example.backblog.task;

import com.example.backblog.dao.BlogMapper;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class CleanES {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private RestHighLevelClient client;
    @Scheduled(cron = "0 1 * * * *") // 每小时的第一分钟执行
    public void doSomethingAtNoon() throws IOException {
        // 查询所有deleteflag为true的数据
        List<Long> deleteId = blogMapper.findDeleteId();
        for (Long aLong : deleteId) {
            DeleteRequest request = new DeleteRequest("blog", "doc", aLong+"");
            client.delete(request, RequestOptions.DEFAULT);
            // 同时删除数据
            blogMapper.deleteById(aLong);
        }
    }
}
